package model.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Professor;

public class ProfessorDAO {
	
	private EntityManager manager = JPAUtil.getEntityManager();
	
	
	public static ProfessorDAO getInstance(){
		return new ProfessorDAO();
	}

	private ProfessorDAO() {
	}
	
	public void adiciona(Professor Professor){
		JPAUtil.iniciaTransacao();
		manager.persist(Professor);
		JPAUtil.finalizaTransacao();
	}
	
	public Professor buscaProfessor(String login){
		Query query = manager.createQuery("SELECT p FROM Professor p WHERE p.login = ?");
		query.setParameter(1, login);
		Professor professor = null;
		if (query.getResultList()!=null && !query.getResultList().isEmpty())
			professor = (model.Professor) query.getResultList().get(0);
		
		return professor;
		
	}
	
	public List<Professor> buscarTodosProfessor(){
		Query query = manager.createQuery("SELECT p FROM Professor p");
		return query.getResultList();
	}
}
